package com.hw.core.system.service;

import com.hw.core.system.entity.NoteBook;
import com.hw.core.system.esRepository.NoteBookESRepository;
import com.hw.core.system.repository.NoteBookRepository;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class NoteBookESService {

    @Autowired
    private NoteBookESRepository noteBookESRepository;
    @Autowired
    private NoteBookRepository noteBookRepository;

    public void addAll() {
        List<NoteBook> all = noteBookRepository.findAll();
        for (NoteBook noteBook : all) {
            noteBook.setUser( null );

            noteBookESRepository.save( noteBook );
        }
    }

    public void deleteAll() {
        noteBookESRepository.deleteAll();
    }

    public Page<NoteBook> find(String key, Integer page, Integer size) {
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder( key );
        PageRequest pageRequest = new PageRequest( page - 1, size );
        return noteBookESRepository.search( builder, pageRequest );

    }

}
